//
//  UIViewController+LCMobShare.m
//  JKFuYinProject
//
//  Created by admin on 2018/2/7.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "UIViewController+LCMobShare.h"
#import <ShareSDKUI/ShareSDK+SSUI.h>
#import "AlertKuangView.h"
#import "UIViewController+LCLoadData.h"

@implementation UIViewController (LCMobShare)

#pragma mark - 分享
-(void)shareShengJingMessageWithImageArray:(NSArray *)imageArray
                                           url:(NSString *)url
                                         title:(NSString *)title
                                contentMessage:(NSString *)messageBody
                                          type:(SSDKContentType)type{
    if ([ShareSDK isClientInstalled:SSDKPlatformTypeWechat] == NO) {
        [SVProgressHUD showErrorWithStatus:@"未安装微信"];
        //[SVProgressHUD dismissWithDelay:2];
        return;
    }

    //1、创建分享参数
    if (imageArray) {
        
    }else{
        imageArray = @[[UIImage imageNamed:@"我的_设置_关于"]];
    }
    if (url) {
        
    }else{
        url = @"http://www.9ku.com/down/fuyin/pc/";
    }
    if (title) {
        //
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:messageBody
                                         images:imageArray
                                            url:[NSURL URLWithString:url]
                                          title:title
                                           type:type];
        //            //2、分享（可以弹出我们的分享菜单和编辑界面）
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{//-[UIApplication canOpenURL:] must be used from main thread only
            
            [ShareSDK showShareActionSheet:nil items:@[
                                                       @(SSDKPlatformSubTypeWechatSession),
                                                       @(SSDKPlatformSubTypeWechatTimeline)
//                                                       @(SSDKPlatformTypeQQ),
//                                                       @(SSDKPlatformTypeSinaWeibo)
                                                       ]
                               shareParams:shareParams onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                
                switch (state) {
                    case SSDKResponseStateSuccess:
                    {
                        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"分享成功"
                                                                                       message:nil
                                                                                preferredStyle:UIAlertControllerStyleAlert];
                        
                        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                                              handler:^(UIAlertAction * action) {}];
                        
                        [alert addAction:defaultAction];
                        
                        [self presentViewController:alert animated:YES completion:nil];
                        break;
                    }
                    case SSDKResponseStateFail:
                    {
                        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"分享失败"
                                                                                       message:[NSString stringWithFormat:@"%@",error]
                                                                                preferredStyle:UIAlertControllerStyleAlert];
                        
                        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                                              handler:^(UIAlertAction * action) {}];
                        
                        [alert addAction:defaultAction];
                        [self presentViewController:alert animated:YES completion:nil];
                        break;
                    }
                    default:
                        break;
                }
                
            }];
            
        }];
    }
}

- (void)shareAudioWithImageArray:(NSArray *)imageArray
                                       url:(NSString *)url
                              musicWebURL:(NSString *)musicWebURL
                                     title:(NSString *)title
                            contentMessage:(NSString *)messageBody
                                      type:(SSDKContentType)type{
    if ([ShareSDK isClientInstalled:SSDKPlatformTypeWechat] == NO) {
        [SVProgressHUD showErrorWithStatus:@"未安装微信"];
        //[SVProgressHUD dismissWithDelay:2];
        return;
    }
    
    //1、创建分享参数
    if (imageArray) {
        
    }else{
        imageArray = @[@"http://fuyin-api.app.inping.com/pic/shareAudioDefault.png"];
    }
    if (url) {
        
    }else{
        url = @"http://www.9ku.com/down/fuyin/pc/";
    }
    if (title) {
        //
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:nil
                                         images:@[[UIImage imageNamed:@"我的_设置_关于"]]
                                            url:nil
                                          title:nil
                                           type:SSDKContentTypeAudio];

        [shareParams SSDKSetupWeChatParamsByText:messageBody title:title url:[NSURL URLWithString:musicWebURL] thumbImage:imageArray[0] image:nil musicFileURL:[NSURL URLWithString:url] extInfo:nil
                                        fileData:nil emoticonData:nil type:SSDKContentTypeAudio forPlatformSubType:SSDKPlatformSubTypeWechatSession];
        [shareParams SSDKSetupWeChatParamsByText:messageBody title:title url:[NSURL URLWithString:musicWebURL] thumbImage:imageArray[0] image:nil musicFileURL:[NSURL URLWithString:url] extInfo:nil
                                        fileData:nil emoticonData:nil type:SSDKContentTypeAudio forPlatformSubType:SSDKPlatformSubTypeWechatTimeline];

        //            //2、分享（可以弹出我们的分享菜单和编辑界面）
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{//-[UIApplication canOpenURL:] must be used from main thread only
            [ShareSDK showShareActionSheet:nil items:@[
                                                       @(SSDKPlatformSubTypeWechatSession),
                                                       @(SSDKPlatformSubTypeWechatTimeline)
                                                       ]
                               shareParams:shareParams onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                                   
                                   switch (state) {
                                       case SSDKResponseStateSuccess:
                                       {
                                       AlertKuangView * alert = [AlertKuangView new];
                                       [alert moveJingGaoKuang:@"分享成功!" :self.view];
                                       break;
                                       }
                                       case SSDKResponseStateFail:
                                       {
                                       AlertKuangView * alert = [AlertKuangView new];
                                       [alert moveJingGaoKuang:@"分享失败!" :self.view];
                                           break;
                                       }
                                       default:
                                           break;
                                   }
                                   
                               }];
            
        }];
    }
}
/**
 *  platformType 平台类型
 */
- (void)shareNoUIWithImageArray:(NSArray *)imageArray
                           url:(NSString *)url
                         title:(NSString *)title
                contentMessage:(NSString *)messageBody
                          type:(SSDKContentType)type
                  platformType:(SSDKPlatformType)platformType
{
    if ([ShareSDK isClientInstalled:SSDKPlatformTypeWechat] == NO) {
        [SVProgressHUD showErrorWithStatus:@"未安装微信"];
        //[SVProgressHUD dismissWithDelay:2];
        return;
    }
    //1、创建分享参数
    if (imageArray) {
        
    }else{
        imageArray = @[[UIImage imageNamed:@"我的_设置_关于"]];
    }
    if (url) {
        
    }else{
        url = @"http://www.9ku.com/down/fuyin/pc/";
    }
    if (title) {
        //
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:messageBody
                                     images:imageArray
                                        url:[NSURL URLWithString:url]
                                      title:title
                                       type:type];
        //进行分享
        [ShareSDK share:platformType //传入分享的平台类型
         parameters:shareParams
        onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
         
        }];
    }
}

- (void)shareArticleAndImageArray:(NSArray *)imageArray
                               url:(NSString *)url
                             title:(NSString *)title
                    contentMessage:(NSString *)messageBody
                              type:(SSDKContentType)type
                                tid:(NSString *)tid
{
    if ([ShareSDK isClientInstalled:SSDKPlatformTypeWechat] == NO) {
        [SVProgressHUD showErrorWithStatus:@"未安装微信"];
        //[SVProgressHUD dismissWithDelay:2];
        return;
    }
    
    //1、创建分享参数
    if (imageArray) {
        
    }else{
        imageArray = @[[UIImage imageNamed:@"我的_设置_关于"]];
    }
    if (url) {
        
    }else{
        url = @"http://www.9ku.com/down/fuyin/pc/";
    }
    if (title) {
        //
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:messageBody
                                         images:imageArray
                                            url:[NSURL URLWithString:url]
                                          title:title
                                           type:type];
        //            //2、分享（可以弹出我们的分享菜单和编辑界面）
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{//-[UIApplication canOpenURL:] must be used from main thread only
            
            [ShareSDK showShareActionSheet:nil items:@[
                                                       @(SSDKPlatformSubTypeWechatSession),
                                                       @(SSDKPlatformSubTypeWechatTimeline)
                                                       //                                                       @(SSDKPlatformTypeQQ),
                                                       //                                                       @(SSDKPlatformTypeSinaWeibo)
                                                       ]
                               shareParams:shareParams onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                                   
                                   switch (state) {
                                       case SSDKResponseStateSuccess:
                                       {
                                           //@"分享成功"
                                           [self addLCBeautifulEssayControllerLingShareIncomesDataWithTid:tid];
                                           break;
                                       }
                                       case SSDKResponseStateFail:
                                       {
                                           UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"分享失败"
                                                                                                          message:[NSString stringWithFormat:@"%@",error]
                                                                                                   preferredStyle:UIAlertControllerStyleAlert];
                                           
                                           UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                                                                 handler:^(UIAlertAction * action) {}];
                                           
                                           [alert addAction:defaultAction];
                                           [self presentViewController:alert animated:YES completion:nil];
                                           break;
                                       }
                                       default:
                                           break;
                                   }
                                   
                               }];
            
        }];
    }
}

@end
